/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *		pub_serial.cpp
 * 位 置:
 *		src/biz/pub_serial.cpp
 * 描 述:
 *		系统流水类
 * 备 注:
 *		无
 * 修 改:
 *		2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include <exception>
#include "occi.h"
#include "pub/log.h"
#include "biz/pub_serial.h"

using namespace std;
using namespace oracle::occi;

//----------------------------------------------
//  构造函数
//----------------------------------------------
PubSerial::PubSerial(Connection* conn)
{
	this->conn = conn;
}

//----------------------------------------------
//  析构函数
//----------------------------------------------
PubSerial::~PubSerial()
{
	this->conn = 0;
}

//----------------------------------------------
//  生成委托流水
//----------------------------------------------
void PubSerial::genOrderSerial()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("生成委托流水...");
		stmt->setSQL("select SEQ_ORDERSERIAL.nextval from dual");
		rs = stmt->executeQuery();
		rs->next();
		orderSerialNo = rs->getString(1);
		orderSerialNo = tradeDate + orderSerialNo.lpadz(8);
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		Log::debug("委托流水: [%s]", orderSerialNo.c_str());
	}
	catch (exception& e)
	{
		Log::error("生成委托流水出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  生成申请流水
//----------------------------------------------
void PubSerial::genAppSerial()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("生成申请流水...");
		stmt->setSQL("select SEQ_APPSERIAL.nextval from dual");
		rs = stmt->executeQuery();
		rs->next();
		appSerialNo = rs->getString(1);
		appSerialNo = tradeDate + appSerialNo.lpadz(8);
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		Log::debug("申请流水: [%s]", appSerialNo.c_str());
	}
	catch (exception& e)
	{
		Log::error("生成申请流水出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}
//----------------------------------------------
//  生成确认流水
//----------------------------------------------
void PubSerial::genAckSerial()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("生成确认流水...");
		stmt->setSQL("select SEQ_ACKSERIAL.nextval from dual");
		rs = stmt->executeQuery();
		rs->next();
		ackSerialNo = rs->getString(1);
		ackSerialNo = tradeDate + ackSerialNo.lpadz(8);
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		Log::debug("确认流水: [%s]", ackSerialNo.c_str());
	}
	catch (exception& e)
	{
		Log::error("生成确认流水出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  生成日志流水
//----------------------------------------------
void PubSerial::genLogSerial()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		Log::debug("生成日志流水...");
		stmt->setSQL("select SEQ_OPLOGSERIAL.nextval from dual");
		rs = stmt->executeQuery();
		rs->next();
		logSerialNo = rs->getString(1);
		logSerialNo = tradeDate + logSerialNo.lpadz(8);
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		Log::debug("日志流水: [%s]", logSerialNo.c_str());
	}
	catch (exception& e)
	{
		Log::error("生成日志流水出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}
}

//----------------------------------------------
//  生成资金明细流水
//----------------------------------------------
void PubSerial::genMoneySerial()
{
	cout << "genMoneySerial!";
}

//----------------------------------------------
//  生成汇款凭证流水
//----------------------------------------------
void PubSerial::genRemitSerial()
{
	cout << "genRemitSerial!";
}

//----------------------------------------------
//  生成资金转帐流水
//----------------------------------------------
void PubSerial::genTransferSerial()
{
	cout << "genTransferSerial!";
}
